#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include<vector>
using namespace std;
int main() {
	int n;
	cin >> n;
	int m = n;
	vector<int> v1;
	v1.reserve(n + 1);
	//cout<<n<<endl;
	int i = 0;
	while (m--) {
		cin >> v1[i++];
	}
	v1[i] = 0;
	//cout<<v1.size()<<endl;
	// for (int i = 0; i<n; i++) {
	// cout<<v1[i];
	// }
	int cur = 0;
	int dest = 1;
	long long sum1 = 0;
	long long sum2 = 0;
	long long sum = 0;
	if (n == 1)
	{
		cout << v1[0] << endl;
		return 0;
	}
	while (dest < n) {
		while (dest < n && v1[cur] <= v1[dest]) {
			sum1 += (v1[cur]);
			cur++;
			dest++;
			if (dest >= n || v1[cur] > v1[dest])
			{
				sum1 += v1[cur];
				break;
			}
		}
		while (dest < n && v1[cur] >= v1[dest]) {
			sum2 += (v1[cur]);
			cur++;
			dest++;
			if (dest >= n || v1[cur] < v1[dest])
			{
				sum2 += v1[cur];
				break;
			}
		}
		if (sum1 > sum2)
			sum = sum1;
		else
			sum = sum2;
		sum1 = sum2 = 0;
	}
	cout << sum;
}
